package com.googlecode.dgwt.rebind.base;

import java.io.File;
import java.io.PrintWriter;

import com.google.gwt.core.ext.GeneratorContext;
import com.google.gwt.core.ext.TreeLogger;
import com.google.gwt.core.ext.UnableToCompleteException;
import com.google.gwt.core.ext.TreeLogger.Type;
import com.google.gwt.core.ext.typeinfo.JClassType;
import com.google.gwt.core.ext.typeinfo.TypeOracle;
import com.google.gwt.user.rebind.AbstractSourceCreator;
import com.google.gwt.user.rebind.ClassSourceFileComposerFactory;
import com.google.gwt.user.rebind.SourceWriter;

/**
 * @author      Wellington Tadeu dos Santos
 * @since       DGWT v0.01
 */
public abstract class BaseSourceCreator extends AbstractSourceCreator {

    protected final TreeLogger logger;
    protected final TypeOracle typeOracle;
    protected final GeneratorContext context;

    public BaseSourceCreator(GeneratorContext context, TreeLogger logger, TypeOracle typeOracle) {
        this.logger = logger;
        this.context = context;
        this.typeOracle = typeOracle;
    }

    
    public abstract String emitClass(JClassType aTargetClass) throws UnableToCompleteException;
    
    
    protected SourceWriter prepareSourceWriter(ClassSourceFileComposerFactory factory, SourceWriter writer, String packageName, String newClassName) {
        // FIXME  remover em produção
        //*
        try {
            File file = new File("C:/Workspace/WELL/Temp/" + packageName.replace('.', '/') + "/");
            file.mkdirs();
            writer = new DoubleSourceWriter(writer, factory.createSourceWriter(new PrintWriter(new File(file, newClassName + ".java"))));
        } catch (Exception e) {
            logger.log(Type.WARN, e.getMessage());
        }
        //*/
        return writer;
    }
    
}
